package com.wanmait.myhouse.filter;

import com.wanmait.myhouse.vo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/web/*")
public class UserLoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String uri = httpServletRequest.getRequestURI();
        if (uri.contains("/login")||uri.contains(".jpg")||uri.contains(".gif")||uri.contains(".png")||uri.contains(".css")||uri.contains("verifyLogin")){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        HttpSession session = httpServletRequest.getSession();
        Object o = session.getAttribute("user");
        User user = (User) o;
        if (user != null)
        {
            if (user.getUserBan()){
                filterChain.doFilter(servletRequest, servletResponse);
            }else {
                if (!uri.contains("/contact")){
                    filterChain.doFilter(servletRequest, servletResponse);
                }else {
                    HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
                    httpServletResponse.sendRedirect("/myhouse/web/mistake");
                }
            }

        }else{
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
}
